﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 语言翻译
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-09-05
    /// </summary>
    public class LanguageMapBll
    {
        private LanguageMapIService service = new LanguageMapService();
        private DictionaryDataBll dictionaryDataBll = new DictionaryDataBll();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <param name="languageTypeId">分类主键</param>
        /// <returns></returns>
        public async Task<PageResult> GetList(PageInput input, string languageTypeId)
        {
            try
            {
                string[] language = (await dictionaryDataBll.GetList("dc6b2542d94b407cac61ec1d59592901")).Select(m => m.EnCode).ToArray();
                return await service.GetList(input, languageTypeId, language);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="fnCode">编码</param>
        /// <returns></returns>
        public async Task<List<LanguageMapEntity>> GetList(string fnCode)
        {
            try
            {
                return await service.GetList(fnCode);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 重复验证（翻译标记）
        /// </summary>
        /// <param name="fnCode">编码</param>
        /// <param name="signKey">翻译标记</param>
        /// <returns></returns>
        public async Task<bool> IsExistBySignKey(string fnCode, string signKey)
        {
            try
            {
                return await service.IsExistBySignKey(fnCode, signKey);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fnCode">编码</param>
        public async Task Delete(string fnCode)
        {
            try
            {
                await service.Delete(fnCode);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entitys">实体对象</param>
        public async Task Create(List<LanguageMapEntity> entitys)
        {
            try
            {
                var fnCode = CommonHelper.Guid;
                var userId = UserProvider.Instance.Get().UserId;
                var creatorTime = DateTime.Now;
                var sortCode = CommonHelper.SortCode;
                foreach (var entity in entitys)
                {
                    entity.Id = CommonHelper.Guid;
                    entity.EnCode = fnCode;
                    entity.SortCode = sortCode;
                    entity.CreatorTime = creatorTime;
                    entity.CreatorUserId = userId;
                }
                await service.Create(fnCode, entitys);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="fnCode">编码</param>
        /// <param name="entitys">实体对象</param>
        public async Task Update(string fnCode, List<LanguageMapEntity> entitys)
        {
            try
            {
                var beforeLanguageMap = (await this.GetList(fnCode)).FirstOrDefault();
                var creatorUserId = beforeLanguageMap.CreatorUserId;
                var creatorTime = beforeLanguageMap.CreatorTime;
                var sortCode = beforeLanguageMap.SortCode;
                foreach (var entity in entitys)
                {
                    entity.Id = CommonHelper.Guid;
                    entity.EnCode = fnCode;
                    entity.SortCode = sortCode;
                    entity.CreatorTime = creatorTime;
                    entity.CreatorUserId = creatorUserId;
                }
                await service.Create(fnCode, entitys);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

    }
}